import random
from PyQt5.QtWidgets import *
from myutil import globalvar as gl, logs
from ui.send_feedback_form import Ui_Frame as send_feedback_frame


class send_feedback_form(QFrame, send_feedback_frame):
    def __init__(self, parent=None):
        super(send_feedback_form, self).__init__(parent)
        self.xjxx_dict = gl.get_value("xjxx_dict")
        self.setupUi(self)

        self.inti()

        self.submit_pushButton.clicked.connect(self.submit_event)

    def submit_event(self):
        logs.logs_print("提交建议")
        date = str(self.textEdit.toPlainText())
        if self.xjxx_dict is None:
            try:
                mysql = gl.get_value("mysql_diver")
                mysql.add_one("INSERT INTO user_jy(`key`, `value`) VALUES (%s, %s);",
                              ["{}_jy_{}".format("weizhi", random.randint(1000000, 9999999)), date])
                self.submit_pushButton.setText("提交成功")
            except:
                pass
        else:
            try:
                mysql = gl.get_value("mysql_diver")
                mysql.add_one("INSERT INTO user_jy(`key`, `value`) VALUES (%s, %s);",
                              ["{}_jy_{}".format(self.xjxx_dict["学号"], random.randint(1000000, 9999999)), date])
                self.submit_pushButton.setText("提交成功")
            except:
                pass

    def inti(self):
        if self.xjxx_dict is None:
            self.user_num_label.setText("建议登录以后再提建议，不然没有反馈")
        else:
            user_num = self.xjxx_dict["学号"]
            self.user_num_label.setText(user_num)
            try:
                mysql = gl.get_value("mysql_diver")
                res = mysql.get_find_list("select `value` from user_jy where `key`='{}_hui'".format(user_num))
                self.feedback_res_label.setText(res[0]["value"])
            except:
                pass
